<template>
	<view v-if="cartData.payPrice">
		<view @click="isEdit=!isEdit">
			<text v-if="isEdit">取消</text>
			<text v-else>编辑</text>
		</view>
		<view class="produce flex" v-for="produce in cartData.skuGroupList" :key="produce.mainSku.skuId">
			<view class="col-10" v-if="isEdit" @click="removeCart(produce.mainSku.skuId)">
				删除
			</view>
			<view v-else class="col-10">
				<switch type="checkbox" :checked="produce.mainSku.checked" @change="checkCart(produce.mainSku.skuId,$event.detail.value)"></switch>
			</view>
			<view class="col-30">
				<image :src="produce.mainSku.skuImage"></image>
			</view>
			<view class="col-60">
				<view>{{produce.mainSku.skuName}}</view>
				<view class="flex" style="justify-content: space-between;">
					<view class="price">
						<text>{{produce.mainSku.skuOriginalPrice}}--</text>
						<text>{{produce.mainSku.skuPrice}}</text>
					</view>
					<view>x {{produce.mainSku.count}}</view>
				
				</view>
			</view>
		</view>
		<view  >
			<view>
				总价格：{{cartData.payPrice}}
			</view>
			<view>总选择{{cartData.totalCheckedCount}}</view>
			<navigator url="/pages/create/create">确认订单</navigator>
		</view>
	</view>
</template>
<script setup>
	import {ref} from 'vue';
	import {RemoveCart,GetCartList,CheckCart} from '../../api/index.js'
	import {onShow} from '@dcloudio/uni-app'
	// 定义是否编辑
	const isEdit = ref(false);
	// 购物车数据
	const cartData = ref({});
	// 获取购物车类别
	function getCart(){
		GetCartList()
		.then(res=>{
			// 获取购物车数据
			cartData.value = res.data;
			// 如果有数据
			if(res.data.totalCheckedCount){
				// 显示底部栏小红点，从第0开始第2个
				uni.setTabBarBadge({
					index:2,//下标
					text:res.data.totalCheckedCount // 内容
				})
			}
			
		})
		
	}
	// 选择购物车
	function checkCart(id,val){
		// 修改参数 id是skuId，checked 0或者1
		var params = {"mainSkuId":id,"checked":val*1}
		CheckCart(params)
		.then(res=>{
			// 重新获取购物车
			getCart()
		})
	}
	// 删除购物车
	function removeCart(id,){
		// 获取删除参数
		var params = {"mainSkuId":id}
		// 删除
		RemoveCart(params)
		.then(res=>{
			// 删除完毕重新获取购物车
			getCart()
		})
	}
	
	onShow(()=>{
		// 当页面显示就重新获取购物车
		getCart();
	})
</script>
<style>
	.flex{
		display: flex;
	}
	 
	.col-30 image{
		width: 200rpx;
		height: 200rpx;
		 
	}
	.col-10{
		flex-basis: 10%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.col-60{
		flex-basis: 30%;
	}
	.col-60{
		flex-basis: 60%;
	}
</style>